<?php

namespace Modules\Admin\Http\Controllers\SiteMessage;

use Modules\Common\Models\EmployeeUser;
use Modules\Common\Models\EnterpriseUser;
use Modules\Common\Models\SiteMessage;
use Modules\Common\Util\Api;


class Create
{
    /**
     * @Route(path=/api/admin/site-message  method=post)
     */
    public function index()
    {
        Api::validate(['content' => 'required', 'type' => 'required']);

        $title = request('title', '系统消息');
        $content = request('content');

        switch (request('type')) {
            case 1:
                $enterprise_list = EnterpriseUser::get(['id']);
                if (count($enterprise_list)) {
                    foreach ($enterprise_list as $item) {
                        SiteMessage::system2EnterpriseUser($item->id, $content, $title, 1);
                    }
                }
                break;
            case 2:
                $employee_list = EmployeeUser::get(['id']);
                if (count($employee_list)) {
                    foreach ($employee_list as $item) {
                        SiteMessage::system2EmployeeUser($item->id, $content, $title, 1);
                    }
                }
                break;
            case 3:
                $enterprise_list = EnterpriseUser::get(['id']);
                if (count($enterprise_list)) {
                    foreach ($enterprise_list as $item) {
                        SiteMessage::system2EnterpriseUser($item->id, $content, $title, 1);
                    }
                }
                $employee_list = EmployeeUser::get(['id']);
                if (count($employee_list)) {
                    foreach ($employee_list as $item) {
                        SiteMessage::system2EmployeeUser($item->id, $content, $title, 1);
                    }
                }
                break;
        }

        return Api::success();
    }


}
